//
//  ADTableViewController.m
//  何五路
//
//  Created by _ADY on 15/6/27.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "ADTableViewController.h"
#import "GoodsDetailsViewController.h"

@interface ADTableViewController ()

@end
#define sizeHight (screenMySize.size.width-30)/2
@implementation ADTableViewController
@synthesize myTableView,DestionValue,DestionType,allArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"活动促销";
        self.view.backgroundColor = bgColor;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (allArray != nil) {
        return;
    }
    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:myTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [myTableView setTableFooterView:v];
    
    allArray = [[NSMutableArray alloc] init];
    
    
    
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:DestionValue forKey:@"code"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *myArray =nil;
        if ([DestionType isEqualToString:@"3"])
            myArray = [SendService ActiveProducts:postDic];
        else if ([DestionType isEqualToString:@"2"])
            myArray = [SendService GetWindowLayoutProductList:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"myArray %@",myArray);
            if ([DestionType isEqualToString:@"3"])
            {
                for (NSDictionary *pic in [myArray objectForKey:@"Entity"])
                {
                    [allArray addObject:pic];
                    
                }
            }
            else if ([DestionType isEqualToString:@"2"])
            {
                for (NSDictionary *pic in [[myArray objectForKey:@"Entity"]objectForKey:@"WindowProductListModel"])
                {
                    [allArray addObject:pic];
                    
                }
            }
 
            [myTableView reloadData];
        });
    });

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return allArray.count/2+allArray.count%2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60+sizeHight+5;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"screenTwoCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;

    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.backgroundColor = [UIColor clearColor];

    for (int i = 0; i < 2; i++)
    {
        if (indexPath.row*2+i >= allArray.count) {
            return cell;
        }

       NSDictionary *dic = [allArray objectAtIndex:indexPath.row*2+i];
        
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+i*(screenMySize.size.width-10)/2,5, sizeHight,sizeHight)];
        if ([DestionType isEqualToString:@"3"])
        [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[dic objectForKey:@"DefaultPictureModel"]objectForKey:@"ImageUrl"]]]];
        else if ([DestionType isEqualToString:@"2"])
            [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[dic objectForKey:@"Product"]objectForKey:@"BigImageUrl"]]]];
        bgImageView.contentMode =  UIViewContentModeScaleAspectFit;
        bgImageView.backgroundColor  = [UIColor whiteColor];
        [cell.contentView addSubview:bgImageView];
        
        NSString *nameString = nil;
        NSString *ordPrice = nil;
        NSString *Price = nil;
        if ([DestionType isEqualToString:@"3"])
        {
            nameString = [NSString stringWithFormat:@"%@",[dic objectForKey:@"Name"]];
            ordPrice = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"ProductPrice"] objectForKey:@"OldPrice"]];
            Price = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"ProductPrice"] objectForKey:@"Price"]];
        }
        else if ([DestionType isEqualToString:@"2"])
        {
            nameString = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"Product"]objectForKey:@"ProductName"]];
            ordPrice = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"Product"]objectForKey:@"OldPrice"]];
            Price = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"Product"]objectForKey:@"Price"]];
        }
        [cell.contentView addSubview:[self setFrame:CGRectMake(bgImageView.frame.origin.x, bgImageView.frame.origin.y+sizeHight, sizeHight, 40) setTitle:nameString setFont:labelSize setColor:[UIColor grayColor]]];
//

        
        if ([[NSString stringWithFormat:@"%@",ordPrice] isEqualToString:@"<null>"]) {
            [cell.contentView addSubview:[self setFrame:CGRectMake(bgImageView.frame.origin.x, bgImageView.frame.origin.y+sizeHight+40, sizeHight, 20) setTitle:Price setFont:labelSize setColor:[UIColor redColor]]];
        }
        else
        {
            UICustomLineLabel *mlabel = [[UICustomLineLabel alloc] init];
            mlabel.frame = CGRectMake(bgImageView.frame.origin.x, bgImageView.frame.origin.y+sizeHight+40, sizeHight/2, 20);
            mlabel.lineType = LineTypeMiddle;
            mlabel.textColor = [UIColor redColor];
            mlabel.lineColor = [UIColor redColor];
            mlabel.font = [UIFont fontWithName:@"Arial" size:labelSize-2];
            mlabel.text = ordPrice;
            [cell.contentView addSubview:mlabel];
            
            UICustomLineLabel *mlabel1 = [[UICustomLineLabel alloc] init];
            mlabel1.frame = CGRectMake(bgImageView.frame.origin.x+sizeHight/2, bgImageView.frame.origin.y+sizeHight+40, sizeHight/2, 20);
            mlabel1.lineType = LineTypeNone;
            mlabel1.textColor = [UIColor redColor];
            mlabel1.lineColor = [UIColor redColor];
            mlabel1.font = [UIFont fontWithName:@"Arial" size:labelSize];
            mlabel1.text = Price;
            [cell.contentView addSubview:mlabel1];
            
        }
        
        
        UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgButton setFrame:CGRectMake(10+i*(screenMySize.size.width-10)/2,5, sizeHight,sizeHight+60)];
        [bgButton addTarget:self action:@selector(gotoAction:) forControlEvents:UIControlEventTouchUpInside];
        bgButton.tag =indexPath.row*2+i+1000;
        [cell.contentView addSubview:bgButton];
    }
    
    


    // Configure the cell...
    return cell;
}

-(void)gotoAction:(id)sender
{
    int type = ((UIButton*)sender).tag -1000;
    GoodsDetailsViewController *details = [[GoodsDetailsViewController alloc] init];
     if ([DestionType isEqualToString:@"3"])
         details.productId = [NSString stringWithFormat:@"%@",[[[allArray objectAtIndex:type] objectForKey:@"ReviewOverviewModel"] objectForKey:@"ProductId"]];
    else if ([DestionType isEqualToString:@"2"])
        details.productId = [NSString stringWithFormat:@"%@",[[[allArray objectAtIndex:type] objectForKey:@"Product"] objectForKey:@"ProductId"]];
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setFont:(int)font setColor:(UIColor*)myColor
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:font];
    titleLabel.textColor = myColor;
    return titleLabel;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
